package com.jiangjun.fetch.quartz;

import java.util.TimerTask;

import EDU.oswego.cs.dl.util.concurrent.FutureResult;

import com.jiangjun.fetch.FetchRss;
import com.jiangjun.fetch.mongo.MongoDBSupport;
import com.jiangjun.fetch.threadpool.FetchHandleCallable;
import com.jiangjun.fetch.threadpool.FetchRssThreadPool;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class FetchTask extends TimerTask {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		MongoDBSupport mongoDBSupport = MongoDBSupport.getInstance();
		BasicDBObject param = new BasicDBObject();
		param.put("isDelete", "1");
		DBCursor cursor = mongoDBSupport.queryCollection(
				MongoDBSupport._FETCHRSS, param);
		while (cursor.hasNext()) {
			DBObject object = cursor.next();
			FetchRss fetchRss = new FetchRss();
			fetchRss.setId(object.get("id").toString());
			if (null != object.get("url"))
				fetchRss.setUrl(object.get("url").toString());
			if (null != object.get("des"))
				fetchRss.setDes(object.get("des").toString());
			if (null != object.get("xpath"))
				fetchRss.setXpath(object.get("xpath").toString());
			if (null != object.get("isRssXml"))
				fetchRss.setIsRssXml(object.get("isRssXml").toString());
			if (null != object.get("date"))
				fetchRss.setDate(object.get("date").toString());
			if (null != object.get("type"))
				fetchRss.setType(object.get("type").toString());
			try {
				FetchRssThreadPool.execute(new FutureResult()
						.setter(new FetchHandleCallable(fetchRss)));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
